//
//  DiscoverTopPageBar.m
//  XYFMApp
//
//  Created by 岁变 on 16/8/31.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "DiscoverTopPageBar.h"
#import "XYMacro.h"

@interface DiscoverTopPageBar () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) NSArray *arrTitleName;


@end

@implementation DiscoverTopPageBar

- (instancetype)initWithFrame:(CGRect)frame withTitleArr:(NSArray *)arr {
    
    self = [super initWithFrame:frame];
    if (self) {
        _arrTitleName = [NSArray arrayWithArray:arr];
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(SCREEN_WIDTH / arr.count, self.frame.size.height);
        layout.minimumLineSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:layout];
        [self addSubview:_collectionView];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor colorWithRed:0.08 green:0.09 blue:0.12 alpha:1.00];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[DiscoverCollectionViewCellForTopPageBar class] forCellWithReuseIdentifier:@"DiscoverCollectionViewCellForTopPageBar"];
        
        self.redLineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 3, SCREEN_WIDTH / arr.count, 2)];
        _redLineView.backgroundColor = [UIColor redColor];
        [_collectionView addSubview:_redLineView];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 1, SCREEN_WIDTH , 1)];
        lineView.backgroundColor = [UIColor colorWithRed:0.20 green:0.21 blue:0.23 alpha:1.00];
        [_collectionView addSubview:lineView];
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _arrTitleName.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DiscoverCollectionViewCellForTopPageBar *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverCollectionViewCellForTopPageBar" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        cell.titleLabel.textColor = [UIColor whiteColor];
    }
    cell.titleLabel.text = _arrTitleName[indexPath.row];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.delegate respondsToSelector:@selector(didClickItemindexPath:PageBar:)]) {
        [self.delegate didClickItemindexPath:indexPath PageBar:self];
    }
}

@end
